
package member

import (
    "time"
    "dhfshop/models"
	"github.com/astaxie/beego/orm"
)

// 会员管理配置参数
type AccountConf struct {
	PartnerId             int64      `orm:"column(partner_id);pk" json:"partner_id" form:"partner_id"`
	
	/* 兑换1元所需要的积分 */
	IntegralExchangeRate  float32  `orm:"column(integral_exchange_rate);digits(12);decimals(2)" json:"integral_exchange_rate" form:"integral_exchange_rate"` 
	/*抵扣1元所需要的积分 */
	IntegralDiscountRate  float32  `orm:"column(integral_discount_rate);digits(12);decimals(2)" json:"integral_discount_rate" form:"integral_discount_rate"`    
	/*消费1元产生的经验 */
	ExperienceRateByOrder float32  `orm:"column(experience_rate_by_order);digits(12);decimals(2)" json:"experience_rate_by_order" form:"experience_rate_by_order"` 
	/* 消费1元产生的积分 */
	IntegralRateByConsumption float32 `orm:"column(integral_rate_by_consumption);digits(12);decimals(2)" json:"integral_rate_by_consumption" form:"integral_rate_by_consumption"`   
	//每单额外赠送
	IntegralBackExtra     int64      `orm:"column(integral_back_extra)" json:"integral_back_extra" form:"integral_back_extra"`                       
	/* 提现手续费费率 */
	TakeOutCsn    float32  `orm:"column(take_out_csn);digits(12);decimals(2)" json:"take_out_csn" form:"take_out_csn"`      
	/* 单笔最低提现金额 */
	MinCashAmount  float32  `orm:"column(min_cash_amount);digits(12);decimals(2)" json:"min_cash_amount" form:"min_cash_amount"` 
	/* 单笔最高提现金额 */
	MaxCashAmount float32 `orm:"column(max_cash_amount);digits(12);decimals(2)" json:"max_cash_amount" form:"max_cash_amount"` 
	//每日提现次数上限
	MaxCashTimesDay     int      `orm:"column(max_cash_times_day)" json:"max_cash_times_day" form:"max_cash_times_day"`    
	//创建时间
	CreatedAt      time.Time `orm:"auto_now_add;column(created_at);type(datetime)" json:"-" form:"-"`                  
	// 修改时间
	UpdatedAt      time.Time `orm:"auto_now;column(updated_at);type(datetime)" json:"-" form:"-"`                      
}

func InitDefaultAccountConf(partnerId int64)(err error) {
    if ok := CheckAccountConfExist(partnerId); !ok {
	    // 默认设置
		defaultAccountConf := AccountConf{
			PartnerId : partnerId,
			IntegralExchangeRate: 100,     // 兑换1元所需要的积分
			IntegralDiscountRate: 100,     // 抵扣1元所需要的积分
			ExperienceRateByOrder: 1,      // 消费1元产生的经验
			IntegralRateByConsumption: 1,  // 消费1元产生的积分
			IntegralBackExtra: 0,          // 每单额外赠送
			TakeOutCsn:        0.01,       // 提现手续费费率
			MinCashAmount:     0.01,       // 单笔最低提现金额
			MaxCashAmount: 5000,           // 单笔最高提现金额
		}
		_, err = AddAccountConf(&defaultAccountConf)
	}
	return
}

func init() {
    orm.RegisterModel(new(AccountConf))   
}

func AddAccountConf(m *AccountConf) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}
func GetAccountConfById(id int64) (v *AccountConf, err error) {
	o := models.GetDefaultDB()
	v = &AccountConf{PartnerId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func UpdateAccountConfById(m *AccountConf, param ...string) (err error) {
	o := models.GetDefaultDB()
	v := AccountConf{PartnerId: m.PartnerId}
	if err = o.Read(&v); err == nil {
		if _, err = o.Update(m, param...); err == nil {
			return nil
		}
	}
	return
}

func DeleteAccountConf(partnerId int64) (err error) {
	o := models.GetDefaultDB()
	v := AccountConf{PartnerId: partnerId}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return
		}
	}
	return
}

func CheckAccountConfExist(partnerId int64) bool {
	o := models.GetDefaultDB()
	exist := o.QueryTable(new(AccountConf)).Filter("partner_id", partnerId).Exist()
	return exist
}
